package top.cptr.so.aliyundomainserver.service.impl;

import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.cptr.so.aliyundomainserver.aliyunSDKUtils.ClientBeanFactory;
import top.cptr.so.aliyundomainserver.aliyunSDKUtils.exception.CreateClientException;
import top.cptr.so.aliyundomainserver.aliyunSDKUtils.exception.RequestInterfaceException;
import top.cptr.so.aliyundomainserver.service.ResolverManage;

@Service
public class ResolverManageImpl implements ResolverManage {
    @Autowired
    private ClientBeanFactory clientBeanFactory;

    @Override
    public DescribeDomainRecordsResponseBody describeDomainRecords(String token, DescribeDomainRecordsRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        DescribeDomainRecordsResponseBody body;
        try {
            DescribeDomainRecordsResponse domainRecords = client.describeDomainRecords(requestParam);
            body = domainRecords.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }

    @Override
    public DescribeDomainRecordInfoResponseBody describeDomainRecordInfo(String token, DescribeDomainRecordInfoRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        DescribeDomainRecordInfoResponseBody body;
        try {
            DescribeDomainRecordInfoResponse domainRecordInfo = client.describeDomainRecordInfo(requestParam);
            body=domainRecordInfo.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }

    @Override
    public DescribeSubDomainRecordsResponseBody describeSubDomainRecords(String token, DescribeSubDomainRecordsRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        DescribeSubDomainRecordsResponseBody body;
        try {
            DescribeSubDomainRecordsResponse recordsResponse = client.describeSubDomainRecords(requestParam);
            body=recordsResponse.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }

    @Override
    public AddDomainRecordResponseBody addDomainRecord(String token, AddDomainRecordRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        AddDomainRecordResponseBody body;
        try {
            AddDomainRecordResponse recordResponse = client.addDomainRecord(requestParam);
            body = recordResponse.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }

    @Override
    public DeleteDomainRecordResponseBody deleteDomainRecord(String token, DeleteDomainRecordRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        DeleteDomainRecordResponseBody body;
        try {
            DeleteDomainRecordResponse recordResponse = client.deleteDomainRecord(requestParam);
            body=recordResponse.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }

    @Override
    public UpdateDomainRecordResponseBody updateDomainRecord(String token, UpdateDomainRecordRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        UpdateDomainRecordResponseBody body;
        try {
            UpdateDomainRecordResponse recordResponse = client.updateDomainRecord(requestParam);
            body = recordResponse.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }

    @Override
    public DeleteSubDomainRecordsResponseBody deleteSubDomainRecords(String token, DeleteSubDomainRecordsRequest requestParam) throws CreateClientException, RequestInterfaceException {
        Client client = clientBeanFactory.createClient(token);
        DeleteSubDomainRecordsResponseBody body;
        try {
            DeleteSubDomainRecordsResponse recordsResponse = client.deleteSubDomainRecords(requestParam);
            body=recordsResponse.getBody();
        } catch (Exception e) {
            throw new RequestInterfaceException("请求错误",e);
        }
        return body;
    }
}
